package com.sfang.common.util.string;


import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 *
 * des helper
 *
 * @author woxihuan
 *
 */
public class DESHelper {

	private static byte[] keys = { (byte) 0x12, (byte) 0x34, (byte) 0x56,
			(byte) 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF };

	private final static String defaultKey = "#!34*&^$";

	public static void main(String[] args) throws Exception {
		String data = "7777";
		// System.err.println(encrypt(data, key));
		// System.err.println(decrypt(encrypt(data, key), key));
		System.out.println(encrypt(data));
//		System.out.println(decrypt(encrypt(data)));
//		String a="A8A2F14361342295F3D708EB957FFBAC9C9077D17FE7F25C";
//        if (a.equals(encrypt(data)))
//			System.out.println("1");

	}
	/**
	 *
	 * decrypt
	 *
	 * @param message
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String message){
		byte[] retByte = new byte[0];
		try {
			byte[] bytesrc = fromHexString(message.toUpperCase());
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			DESKeySpec desKeySpec = new DESKeySpec(defaultKey.getBytes("UTF-8"));
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			IvParameterSpec iv = new IvParameterSpec(keys);
			cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
			retByte = cipher.doFinal(bytesrc);
			return new String(retByte, "utf-8");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 *
	 * from hexStr to byte array
	 *
	 * @param hexStr
	 * @return
	 */
	private static byte[] fromHexString(String hexStr) {
		byte[] bytesrc = new byte[hexStr.length() / 2];
		for (int i = 0; i < bytesrc.length; i++) {
			String byteString = hexStr.substring(2 * i, 2 * i + 2);
			int byteValue = Integer.parseInt(byteString, 16);
			bytesrc[i] = (byte) byteValue;
		}
		return bytesrc;
	}

	/**
	 *
	 * encrypt
	 *
	 * @param message
	 * @param message
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String message){
		if(message==null){
			return "";
		}
		try {
			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			DESKeySpec desKeySpec = new DESKeySpec(defaultKey.getBytes("UTF-8"));
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			IvParameterSpec iv = new IvParameterSpec(keys);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
			byte[] bytes = cipher.doFinal(message.getBytes("UTF-8"));
			return toHexString(bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 *
	 * from byte array to hex string
	 *
	 * @param bytes
	 * @return
	 */
	private static String toHexString(byte[] bytes) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String plainText = Integer.toHexString(0xff & bytes[i]);
			if (plainText.length() < 2)
				plainText = "0" + plainText;
			hexString.append(plainText);
		}

		return hexString.toString().toUpperCase();
	}


}
